{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "authorship_tag": "ABX9TyMKTHGQxmyyjRDco/uRBJXU",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/Huangjian2013/ai-demo/blob/main/rag/04_Qdrant.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install qdrant-client"
      ],
      "metadata": {
        "id": "moDSivjssz5Z"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "_DNYVZBaOa9N"
      },
      "outputs": [],
      "source": [
        "from qdrant_client import QdrantClient\n",
        "from qdrant_client.models import PointStruct\n",
        "from qdrant_client.models import VectorParams, Distance\n",
        "import numpy\n",
        "\n",
        "collection_name = \"small_collection\"\n",
        "client = QdrantClient(\":memory:\")"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "client.create_collection(\n",
        "  collection_name=collection_name,\n",
        "  vectors_config=VectorParams(size=2, distance=Distance.EUCLID),\n",
        ")"
      ],
      "metadata": {
        "collapsed": true,
        "id": "gF2RGmXEtBmA"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "client.upsert(\n",
        "   collection_name=collection_name,\n",
        "   points=[\n",
        "      PointStruct(\n",
        "            id=1,\n",
        "            vector=numpy.array([5,5], dtype=numpy.float64),\n",
        "            payload={\"car\": \"极越01\"}\n",
        "      ),\n",
        "      PointStruct(\n",
        "            id=2,\n",
        "            vector=numpy.array([10,10], dtype=numpy.float64),\n",
        "            payload={\"car\": \"极越07\"}\n",
        "      )\n",
        "   ]\n",
        ")"
      ],
      "metadata": {
        "id": "CTiNwFSLOcmq"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "hits = client.search(\n",
        "   collection_name=collection_name,\n",
        "   query_vector=numpy.array([6,6], dtype=numpy.float64),\n",
        "   limit=1\n",
        ")\n",
        "\n",
        "print(hits)"
      ],
      "metadata": {
        "id": "_ZLKRZIYwGzD"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}